package com.hao.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.commonutils.R;
import com.hao.eduservice.entity.EduTeacher;
import com.hao.eduservice.entity.vo.TeacherQuery;
import com.hao.eduservice.service.EduTeacherService;
import com.hao.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hao
 * @since 2020-11-19
 */
@Api(tags = {"讲师管理"})
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    /**
     * 找到所有的老师
     *
     * @return {@link List<EduTeacher>}
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("teachers")
    public R findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        try {
            //int a = 1/0;
        }catch (Exception e){
            throw new GuliException(222,"执行自定义异常。。。");
        }
        return R.ok().data("items",list);
    }

    /**
     * 删除老师通过id
     *
     * @param id id
     * @return {@link Boolean}
     */
    @ApiOperation(value = "通过id逻辑删除一个讲师")
    @DeleteMapping("{id}")
    public R deleteTeacherById(@ApiParam(name = "id",value = "讲师id",required = true) @PathVariable String id){
        boolean falg = teacherService.removeById(id);
        if (falg){
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     * 老师列表，分页
     *
     * @param current 当前的
     * @param limit   限制
     * @return {@link R}
     */
    @ApiOperation(value = "讲师分页列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current",value = "当前页数",required = true) @PathVariable long current,
                             @ApiParam(name = "limit",value = "当前页显示数量",required = true) @PathVariable long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        teacherService.page(pageTeacher,null);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }


    @ApiOperation(value = "讲师条件查询分页列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "current",value = "当前页数",required = true) @PathVariable long current,
                                  @ApiParam(name = "limit",value = "当前页显示数量",required = true) @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        teacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 添加讲师接口的方法
     *
     * @param eduTeacher edu老师
     * @return {@link R}
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 通过id查询老师
     *
     * @param id id
     * @return {@link R}
     */
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    /**
     * 更新老师
     *
     * @param eduTeacher edu老师
     * @return {@link R}
     */
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

